package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.project_template.entity.PostLike;
import com.demo.project_template.entity.PostNotice;
import com.demo.project_template.mapper.PostLikeMapper;
import com.demo.project_template.mapper.PostMapper;
import com.demo.project_template.mapper.PostNoticeMapper;
import com.demo.project_template.service.PostLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 帖子点赞 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:57
 */
@Service
public class PostLikeServiceImpl extends ServiceImpl<PostLikeMapper, PostLike> implements PostLikeService {
    @Resource
    private PostNoticeMapper postNoticeMapper;
    @Resource
    private PostMapper postMapper;

    @Override
    public void add(PostLike postLike) {
        PostLike one = getOne(new LambdaQueryWrapper<PostLike>().eq(PostLike::getPostId, postLike.getPostId()).eq(PostLike::getUserId, postLike.getUserId()));
        //如果已有点赞记录则取消点赞
        if (one != null){
            removeById(one);
            return;
        }
        //如果未点赞则进行点赞
        save(postLike);

        //帖子点赞量+1
        postMapper.addLikeCount(postLike.getPostId());

        //添加消息通知
        PostNotice postNotice = new PostNotice();
        postNotice.setPostId(postLike.getPostId());
        postNotice.setUserId(postLike.getUserId());
        postNotice.setType("1");
        postNotice.setState("0");
        postNoticeMapper.insert(postNotice);
    }
}
